package com.tancire.member.service.impl;

import java.util.List;
import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import com.tancire.common.enums.PurseStatus;
import com.tancire.common.exception.question.TrainException;
import com.tancire.common.utils.DateUtils;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.member.domain.Member;
import com.tancire.member.domain.MemberPurse;
import com.tancire.member.service.IMemberPurseService;
import com.tancire.member.service.IMemberService;
import org.springframework.stereotype.Service;
import com.tancire.member.mapper.MemberTaskMapper;
import com.tancire.member.domain.MemberTask;
import com.tancire.member.service.IMemberTaskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员任务Service业务层处理
 *
 * @author tancire
 * @date 2024-10-29
 */
@Service
public class MemberTaskServiceImpl implements IMemberTaskService {

    @Resource
    private MemberTaskMapper memberTaskMapper;

    @Resource
    private IMemberService memberService;

    @Resource
    private IMemberPurseService memberPurseService;

    /**
     * 查询会员任务
     *
     * @param id 会员任务主键
     * @return 会员任务
     */
    @Override
    public MemberTask selectMemberTaskById(Long id) {
        return memberTaskMapper.selectMemberTaskById(id);
    }

    /**
     * 查询会员任务列表
     *
     * @param memberTask 会员任务
     * @return 会员任务
     */
    @Override
    public List<MemberTask> selectMemberTaskList(MemberTask memberTask) {
        return memberTaskMapper.selectMemberTaskList(memberTask);
    }

    /**
     * 新增会员任务
     *
     * @param memberTask 会员任务
     * @return 结果
     */
    @Override
    public int insertMemberTask(MemberTask memberTask) {
        memberTask.setCreateTime(DateUtils.getNowDate());
        return memberTaskMapper.insertMemberTask(memberTask);
    }

    /**
     * 修改会员任务
     *
     * @param memberTask 会员任务
     * @return 结果
     */
    @Override
    public int updateMemberTask(MemberTask memberTask) {
        memberTask.setUpdateTime(DateUtils.getNowDate());
        return memberTaskMapper.updateMemberTask(memberTask);
    }

    /**
     * 批量删除会员任务
     *
     * @param ids 需要删除的会员任务主键
     * @return 结果
     */
    @Override
    public int deleteMemberTaskByIds(Long[] ids) {
        return memberTaskMapper.deleteMemberTaskByIds(ids);
    }

    /**
     * 删除会员任务信息
     *
     * @param id 会员任务主键
     * @return 结果
     */
    @Override
    public int deleteMemberTaskById(Long id) {
        return memberTaskMapper.deleteMemberTaskById(id);
    }

    /**
     * 新增复活卡
     */
    @Transactional
    @Override
    public void addLiveCard() {
        String tdDay = DateUtils.getTdDay();
        MemberTask query = new MemberTask();
        query.setMemberId(SecurityUtils.getUserId());
        query.setTaskDay(tdDay);
        List<MemberTask> memberTaskList = this.memberTaskMapper.selectMemberTaskList(query);
        // 查询会员信息待用
        Member member = this.memberService.selectMemberByMemberId(SecurityUtils.getUserId());

        MemberTask memberTask = new MemberTask();
        if (ObjectUtil.isNotEmpty(memberTaskList)) {
            memberTask = memberTaskList.get(0);
            Integer shareTask = memberTask.getShareTask();
            if (null == shareTask || 0 == shareTask) {
                MemberTask update = new MemberTask();
                update.setId(memberTask.getId());
                update.setShareTask(1);
                this.memberTaskMapper.updateMemberTask(update);
            } else {
                throw new TrainException("复活卡已经领取过，不能重复领取");
            }
        } else {
            memberTask.setTaskDay(tdDay);
            memberTask.setMemberId(SecurityUtils.getUserId());
            memberTask.setMemberAccount(member.getAccount());
            memberTask.setShareTask(1);
            this.memberTaskMapper.insertMemberTask(memberTask);
        }
        // 获取钱包数据
        MemberPurse memberPurse = this.memberPurseService.selectMemberPurseByMemberId(SecurityUtils.getUserId());
        if (null == memberPurse) {
            // 初始化钱包数据
            memberPurse = new MemberPurse();
            memberPurse.setMemberAccount(member.getAccount());
            memberPurse.setMemberId(member.getMemberId());
            memberPurse.setLiveCard(1);
            memberPurse.setPurseStatus(PurseStatus.NORMAL.getCode());
            this.memberPurseService.insertMemberPurse(memberPurse);
        } else {
            MemberPurse update = new MemberPurse();
            update.setId(memberPurse.getId());
            Integer liveCard = memberPurse.getLiveCard();
            if (null == liveCard) {
                liveCard = 0;
            }
            memberPurse.setLiveCard(liveCard + 1);
        }
    }

    /**
     * 获取做任务情况
     *
     * @return
     */
    @Override
    public MemberTask getTaskInfo() {
        String tdDay = DateUtils.getTdDay();
        MemberTask query = new MemberTask();
        query.setMemberId(SecurityUtils.getUserId());
        query.setTaskDay(tdDay);
        List<MemberTask> memberTaskList = this.memberTaskMapper.selectMemberTaskList(query);
        // 查询会员信息待用
        Member member = this.memberService.selectMemberByMemberId(SecurityUtils.getUserId());
        // 判断是否存在数据
        if (ObjectUtil.isNotEmpty(memberTaskList)) {
            return memberTaskList.get(0);
        } else {
            MemberTask memberTask = new MemberTask();
            memberTask.setTaskDay(tdDay);
            memberTask.setMemberId(SecurityUtils.getUserId());
            memberTask.setMemberAccount(member.getAccount());
            memberTask.setShareTask(0);
            this.memberTaskMapper.insertMemberTask(memberTask);
            return memberTask;
        }
    }

    /**
     * 使用复活卡
     */
    @Override
    public void useLiveCard() {
        MemberPurse memberPurse = this.memberPurseService.selectMemberPurseByMemberId(SecurityUtils.getUserId());
        if (null != memberPurse) {
            Integer liveCard = memberPurse.getLiveCard();
            if (null != liveCard && liveCard > 0) {
                MemberPurse update = new MemberPurse();
                update.setId(memberPurse.getId());
                update.setLiveCard(liveCard - 1);
                this.memberPurseService.updateMemberPurse(update);
                return;
            }
        }
        throw new TrainException("您当前没有复活卡");
    }
}
